/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Blasius
 */
public class Perpustakaan {

    
    public static ArrayList<Publikasi> daftar_publikasi = new ArrayList<Publikasi>();
    public static ArrayList<Anggota> DAFTAR_ANGGOTA = new ArrayList<Anggota>();
 public static ArrayList<Pinjaman> daftar_pinjaman = new ArrayList<Pinjaman>();
  
 



 public static void tambahpubikasi(Publikasi publikasi) {
        daftar_publikasi.add(publikasi);
    }
 
 

    public static void tambahanggota(Anggota anggota){
        DAFTAR_ANGGOTA.add(anggota);
    }
        public static void tambahPinjam(Pinjaman pinjaman) {
       daftar_pinjaman.add(pinjaman);
        }

    public static String bacaDaftarPublikasi() {

        if (daftar_publikasi.isEmpty() == false) {
            String hasil = new String();

            for (int i = 0; i < daftar_publikasi.size(); i++) {
                hasil += daftar_publikasi.get(i).getJudul() + "\n";
            }
            return hasil;

        }

        return "Tidak ada";

    }
     public static void simpanFilePublikasi() throws IOException {
        FileOutputStream outStream = null;
        if (daftar_publikasi.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Publikasi Kosong");
        } else {

            try {
                File outFile = new File("publikasi.dat");
                outStream = new FileOutputStream(outFile);
                ObjectOutputStream outObjectStream = new ObjectOutputStream(outStream);
                outObjectStream.writeObject(daftar_publikasi);
                outStream.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
public static void simpanFileAnggota() throws IOException {
        FileOutputStream outStream = null;
        if (DAFTAR_ANGGOTA.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Anggota Kosong");
        } else {

            try {
                File outFile = new File("anggota.dat");
                outStream = new FileOutputStream(outFile);
                String data = new String();
                for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
                    data += "Id Anggota : " + DAFTAR_ANGGOTA.get(i).getIdAnggota() + "\tNama : " + DAFTAR_ANGGOTA.get(i).getNama() + "\n";
                }
                outStream.write(data.getBytes());
                outStream.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
      public static void simpanFilePinjaman() throws IOException {
        FileOutputStream outStream = null;
        if (daftar_pinjaman.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Pinjaman Kosong");
        } else {

            try {
                File outFile = new File("pinjaman.dat");
                outStream = new FileOutputStream(outFile);
                String data = new String();
                for (int i = 0; i < daftar_pinjaman.size(); i++) {
                    data += "Id Anggota : " + daftar_pinjaman.get(i).getPeminjam().getIdAnggota() + "\tId Koleksi : " + daftar_pinjaman.get(i).getPublikasi().getIdKoleksi() + "\n";
                }
                outStream.write(data.getBytes());
                outStream.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static String bacaDaftarAnggota() {
        if (DAFTAR_ANGGOTA.isEmpty() == false) {
            String hasil = new String();
            for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
                hasil += DAFTAR_ANGGOTA.get(i).getNama() + "\n" + DAFTAR_ANGGOTA.get(i).getIdAnggota();
                return hasil;
            }

        }
        return "kosong";

    }

    
   
    
    public static String bacaDaftarPinjaman() {
        if (daftar_pinjaman.isEmpty() == false) {
            String hasil = new String(); 
            for (int i=0; i<daftar_pinjaman.size(); i++) {
                hasil += daftar_pinjaman.get(i).getClass() + "\n";
            }
            return hasil;
        }
        return "Tidak ada";
        }
    public static void bacaFilePublikasi() {
        try {
            FileInputStream inStream = null;
            File inFile = new File("publikasi.dat");
            inStream = new FileInputStream(inFile);
            ObjectInputStream inObjectStream = null;
            try {
                inObjectStream = new ObjectInputStream(inStream);
            } catch (IOException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                daftar_publikasi = (ArrayList<Publikasi>) inObjectStream.readObject();
            } catch (IOException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void bacaFileAnggota() {
        try {
            FileInputStream inStream = null;
            String data = new String();
            File inFile = new File("anggota.dat");
            try {
                inStream = new FileInputStream(inFile);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            int a;
            while ((a = inStream.read()) != -1) {
                data = data + (char) a;
            }
            inStream.close();
            System.out.println(data);
        } catch (IOException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    public static void bacaFilePinjaman() {
        try {
            FileInputStream inStream = null;
            String data = new String();
            File inFile = new File("pinjaman.dat");
            try {
                inStream = new FileInputStream(inFile);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            int x;
            while ((x = inStream.read()) != -1) {
                data = data + (char) x;
            }
            inStream.close();
            System.out.println(data);
        } catch (IOException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
    
    
    
    
    
    
    
    /**
     *
     * @param publikasi
     */
    public static void tambahtabelpublikasi (Publikasi publikasi) throws SQLException{
Connection conn = oRAcONNECTION.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314015", "MHS115314015");
Statement statement = conn.createStatement();
String sql = "insert into publikasi (id_koleksi, judul) values ('"+publikasi.getIdKoleksi() + "','" + publikasi.getJudul()+"')";
statement.executeUpdate(sql);
conn.close();

    }
      public static void tambahTabelAnggota(Anggota anggota) {
        try {
            Connection conn = oRAcONNECTION.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314033", "MHS115314033");
            java.sql.Statement statement = null;
            statement = conn.createStatement();
            String sql = "insert into anggota (id_anggota,nama) values ('" + anggota.getIdAnggota() + "','" + anggota.getNama() + "')";
            statement.executeUpdate(sql);
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
       public static void simpanTabelAnggota() {
        if (DAFTAR_ANGGOTA.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Anggota Kosong");
        } else {
            Connection conn = oRAcONNECTION.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314033", "MHS115314033");
            try {
                java.sql.Statement statement = conn.createStatement();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
                String sql = "insert into anggota (id_anggota,nama) values ('" + DAFTAR_ANGGOTA.get(i).getIdAnggota() + "','" + DAFTAR_ANGGOTA.get(i).getNama() + "')";
            }
            try {
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
public static void simpanTabelPublikasi() {
        if (daftar_publikasi.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Publikasi Kosong");
        } else {
            Connection conn = oRAcONNECTION.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314033", "MHS115314033");
            try {
                java.sql.Statement statement = conn.createStatement();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            for (int i = 0; i < daftar_publikasi.size(); i++) {
                String sql = "insert into publikasi (id_koleksi,judul,penulis,penerbit,tahun) values ('" + daftar_publikasi.get(i).getIdKoleksi() + "','" + daftar_publikasi.get(i).getJudul()
                        + daftar_publikasi.get(i).getPenulis() + "','" + daftar_publikasi.get(i).getPenerbit() + "','"
                        +daftar_publikasi.get(i).getTahun() + "')";
            }
            try {
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
 public static void simpanTabelPinjaman() {
        if (daftar_pinjaman.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Pinjaman Kosong");
        } else {
            Connection conn = oRAcONNECTION.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314033", "MHS115314033");
            try {
                java.sql.Statement statement = conn.createStatement();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            for (int i = 0; i < daftar_pinjaman.size(); i++) {
                String sql = "insert into pinjaman (id_anggota, id_koleksi) values ('" + daftar_pinjaman.get(i).getPeminjam().getIdAnggota() + "','" + daftar_pinjaman.get(i).getPublikasi().getIdKoleksi() + "')";
            }
            try {
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void bacaTabelPublikasi() throws Exception {
        daftar_publikasi = new ArrayList<Publikasi>();
        Connection conn = oRAcONNECTION.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314033", "MHS115314033");
        java.sql.Statement statement = null;
        try {
            statement = conn.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        String sql = "select * from publikasi";
        ResultSet result = null;
        try {
            result = statement.executeQuery(sql);
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            while (result.next()) {
                Publikasi publikasi = new Publikasi() {

                    public int hitungDenda(int lamaPinjam) {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }

               
                    public int gethitungDenda(int lamaPinjam) {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }

                    @Override
                    public int hitungdenda(int lamapinjam) {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }
                };
                publikasi.setIdKoleksi(result.getString("ID Koleksi"));
                publikasi.setJudul(result.getString("Judul"));
                publikasi.setPenulis(result.getString("Penulis"));
                publikasi.setPenerbit(result.getString("Penerbit"));
                publikasi.setTahun(result.getString("Tahun"));
               daftar_publikasi.add(publikasi);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 public static void bacaTabelAnggota() throws Exception {
        DAFTAR_ANGGOTA = new ArrayList<Anggota>();
        Connection conn = oRAcONNECTION.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314033", "MHS115314033");
        java.sql.Statement statement = null;
        try {
            statement = conn.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        String sql = "select * from anggota";
        ResultSet result = null;
        try {
            result = statement.executeQuery(sql);
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            while (result.next()) {
                Anggota temp = new Anggota() {

                    public int hitungDenda(int lamaPinjam) {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }
                };
                temp.setIdAnggota(result.getString("ID Anggota"));
                temp.setNama(result.getString("Nama"));
                DAFTAR_ANGGOTA.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
  public static void bacaTabelPinjaman() throws Exception {
        daftar_pinjaman = new ArrayList<Pinjaman>();
        Connection conn = oRAcONNECTION.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314033", "MHS115314033");
        java.sql.Statement statement = null;
        try {
            statement = conn.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        String sql = "select * from pinjaman";
        ResultSet result = null;
        try {
            result = statement.executeQuery(sql);
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            while (result.next()) {
                Pinjaman pinjaman = new Pinjaman();

                Anggota agg = new Anggota();
                Publikasi publikasi = new Publikasi() {

                    public int hitungDenda(int lamaPinjam) {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }

                   
                    public int gethitungDenda(int lamaPinjam) {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }

                    @Override
                    public int hitungdenda(int lamapinjam) {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }
                };

                agg.setIdAnggota(result.getString("ID Anggota"));
                publikasi.setIdKoleksi(result.getString("ID Koleksi"));
                pinjaman.setPeminjam(agg);
                pinjaman.setPublikasi(publikasi);
                daftar_pinjaman.add(pinjaman);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void tambahTabelPinjaman(Pinjaman pinjaman) {
        try {
            Connection conn = oRAcONNECTION.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314033", "MHS115314033");
            java.sql.Statement statement = null;
            statement = conn.createStatement();
            String sql = "insert into pinjaman (id_anggota, id_koleksi) values ('" + pinjaman.getPeminjam().getIdAnggota() + "','" + pinjaman.getPublikasi().getIdKoleksi() + "')";
            statement.executeUpdate(sql);
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public static ArrayList<Anggota> cariAnggota(String nama) {
        ArrayList<Anggota> hasil = new ArrayList<Anggota>();
        for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
            Anggota temp = DAFTAR_ANGGOTA.get(i);
            if (temp.getNama().contains(nama)) {
                hasil.add(temp);
            }
        }
        return hasil;
    }

    
    
}
